package com.coffee.takeoutserver.controller;

import com.coffee.takeoutserver.bean.GoodsInfo;
import com.coffee.takeoutserver.bean.GoodsTypeInfo;
import com.coffee.takeoutserver.bean.Seller;
import com.coffee.takeoutserver.bean.User;
import com.coffee.takeoutserver.service.SellerService;
import com.coffee.takeoutserver.service.UserService;
import com.coffee.takeoutserver.util.RequestType;
import com.coffee.takeoutserver.util.ResponseInfo;
import com.google.gson.Gson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class SellerController {

    @Autowired
    SellerService sellerService;
    @Autowired
    UserService userService;

    @RequestMapping("updateSellerById")
    @ResponseBody
    public String updateSellerById(@RequestBody Seller seller) {
        sellerService.updateSellerById(seller);
        Gson gson = new Gson();
        ResponseInfo responseInfo = new ResponseInfo();
        responseInfo.setCode("0");
        Map<String, String> map = new HashMap<>();
        map.put("requestType", RequestType.UpdateSellerById);
        responseInfo.setData(gson.toJson(map));
        return gson.toJson(responseInfo);
    }

    @RequestMapping("getSellerById")
    @ResponseBody
    public String getSellerById(int id) {
        Gson gson = new Gson();
        Seller seller = sellerService.getSellerById(id);
        Map<String,String> map = new HashMap<>();
        ResponseInfo responseInfo = new ResponseInfo();
        responseInfo.setCode("0");
        map.put("seller", gson.toJson(seller));
        map.put("requestType", RequestType.GetSellerById);
        responseInfo.setData(gson.toJson(map));
        return gson.toJson(responseInfo);
    }

    @RequestMapping("becomeSeller")
    @ResponseBody
    public String becomeSeller(int userId,@RequestBody Seller seller) {
        sellerService.insertSeller(seller);
        User user = userService.getUserById(userId);
        seller.setId(seller.getId());
        user.setSellerid(seller.getId());
        userService.updateUser(user);
        Gson gson = new Gson();
        ResponseInfo responseInfo = new ResponseInfo();
        responseInfo.setCode("0");
        Map<String, String> map = new HashMap<>();
        map.put("user", gson.toJson(user));
        map.put("seller", gson.toJson(seller));
        responseInfo.setData(gson.toJson(map));
        return gson.toJson(responseInfo);
    }

    @RequestMapping("getBusinessInfoBySellerId")
    @ResponseBody
    public String getBusinessInfoBySellerId(int sellerId) {
        List<GoodsTypeInfo> goodsTypeInfos = sellerService.getGoodsTypeInfoBySellerId(sellerId);
        List<GoodsInfo> goodsInfos = sellerService.getGoodsInfoBySellerId(sellerId);
        List<GoodsTypeInfoRes> goodsTypeInfoResList = new ArrayList<>();

        for (GoodsTypeInfo goodsTypeInfo : goodsTypeInfos) {
            GoodsTypeInfoRes goodsTypeInfoRes = new GoodsTypeInfoRes();
            goodsTypeInfoRes.id = goodsTypeInfo.getId();
            goodsTypeInfoRes.name = goodsTypeInfo.getName();
            goodsTypeInfoRes.sellerId = goodsTypeInfo.getSellerid();
            goodsTypeInfoRes.info = goodsTypeInfo.getInfo();
            for (GoodsInfo goodsInfo : goodsInfos) {
                if (goodsInfo.getTypeid().equals(goodsTypeInfo.getId())) {
                    goodsTypeInfoRes.list.add(goodsInfo);
                }
            }
            goodsTypeInfoResList.add(goodsTypeInfoRes);
        }
        Gson gson = new Gson();
        ResponseInfo responseInfo = new ResponseInfo();
        responseInfo.setCode("0");
        Map<String, String> map = new HashMap<>();
        map.put("list", gson.toJson(goodsTypeInfoResList));
        responseInfo.setData(gson.toJson(map));
        return gson.toJson(responseInfo);
    }

    class GoodsTypeInfoRes {
        int id;
        String name;
        String info;
        List<GoodsInfo> list = new ArrayList<>();
        int sellerId;
    }
}
